package com.childenglish.mapper;

import com.childenglish.entity.ParentChildRelation;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface ParentChildRelationMapper {

    @Select("SELECT COUNT(*) FROM parent_child_relation WHERE parent_id = #{parentId} AND child_id = #{childId}")
    boolean existsByParentIdAndChildId(Long parentId, Long childId);

    @Select("SELECT child_id FROM parent_child_relation WHERE parent_id = #{parentId}")
    List<Long> selectChildIdsByParentId(Long parentId);

    @Insert("INSERT INTO parent_child_relation(parent_id, child_id, relation_type, status, create_time, update_time) " +
            "VALUES (#{parentId}, #{childId}, #{relationType}, #{status}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(ParentChildRelation relation);

    /**
     * 更新亲子关系
     */
    @Update("UPDATE parent_child_relation SET relation_type = #{relationType}, status = #{status}, " +
            "update_time = #{updateTime} WHERE id = #{id}")
    int update(ParentChildRelation relation);

    /**
     * 根据ID查询亲子关系
     */
    @Select("SELECT * FROM parent_child_relation WHERE id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "childId", column = "child_id"),
            @Result(property = "relationType", column = "relation_type"),
            @Result(property = "status", column = "status"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    ParentChildRelation findById(@Param("id") Long id);

    /**
     * 根据学生ID查询所有家长关系
     */
    @Select("SELECT * FROM parent_child_relation WHERE child_id = #{childId}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "childId", column = "child_id"),
            @Result(property = "relationType", column = "relation_type"),
            @Result(property = "status", column = "status"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<ParentChildRelation> findByChildId(@Param("childId") Long childId);
}